﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace ThreadTest
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            string folder = @"D:\Music\AlbumImg";
            DirectoryInfo dir = new DirectoryInfo(folder);
            ImageCodecInfo ici;
            Encoder enc;
            EncoderParameter ep;
            EncoderParameters epa;

            // Initialize the necessary objects
            ici = GetEncoderInfo("image/jpeg");
            enc = Encoder.Quality;//设置保存质量
            epa = new EncoderParameters(1);

            // Set the compression level
            ep = new EncoderParameter(enc, 50L);//质量等级为25%
            epa.Param[0] = ep;
            foreach (var file in dir.GetFiles())
            {
                if (file.Extension == ".jpg")
                {
                    
                    Bitmap bmp = new Bitmap(file.FullName);                  
                    
                    string fileNmae = file.Name;
                    int newH = bmp.Height / 2;
                    int newW = bmp.Width / 2;
                    Bitmap nbitmpa=new Bitmap(newW,newH,PixelFormat.Format16bppRgb555);

                  

                    Graphics g = Graphics.FromImage(nbitmpa);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                    g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);

                    nbitmpa.Save(@"D:\NewImg\" + fileNmae, ici, epa);
                    g.Dispose();
                }
            }

            Console.ReadKey();
        }
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            return null;
        }

    }
}
